// Copyright (c) 2014 Morten Bakkedal
// This code is published under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;

namespace FuncLib.DualFunctions.Collections.Generic
{
	/// <summary>
	/// Read-only wrapper around <see cref="IDictionary<TKey, TValue>" />.
	/// </summary>
	[Serializable]
	public class ReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
	{
		private IDictionary<TKey, TValue> dictionary;

		public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)
		{
			// Warning! No copying is performed.
			this.dictionary = dictionary;
		}

		public bool ContainsKey(TKey key)
		{
			return dictionary.ContainsKey(key);
		}

		public bool Contains(KeyValuePair<TKey, TValue> item)
		{
			return ((ICollection<KeyValuePair<TKey, TValue>>)dictionary).Contains(item);
		}

		public bool TryGetValue(TKey key, out TValue value)
		{
			return dictionary.TryGetValue(key, out value);
		}

		public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
		{
			((ICollection<KeyValuePair<TKey, TValue>>)dictionary).CopyTo(array, arrayIndex);
		}

		public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
		{
			return dictionary.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return ((IEnumerable)dictionary).GetEnumerator();
		}

		public TValue this[TKey key]
		{
			get
			{
				return dictionary[key];
			}
		}

		public int Count
		{
			get
			{
				return dictionary.Count;
			}
		}

		public ICollection<TKey> Keys
		{
			get
			{
				return dictionary.Keys;
			}
		}

		public ICollection<TValue> Values
		{
			get
			{
				return dictionary.Values;
			}
		}
	}
}
